package vivek.project.pocketmoney.expenses.model
{
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.errors.SQLError;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import vivek.project.pocketmoney.admin.model.AdminConstants;
	import vivek.project.pocketmoney.common.utils.CommonConstants;
	import vivek.project.pocketmoney.common.utils.DateUtils;

	public class AccountLoaderImpl implements AccountLoaderBaseClass
	{
		protected var categoryMatch:Array=new Array();

		public function AccountLoaderImpl()
		{
		}

		public function checkValiditiy(fileContent:String):Boolean
		{
			return false;
		}

		public function getExpenseRecords(fileContent:String, account:Object):Array
		{
			return null;
		}

		//public var delimiter:String="\t"
//		public var numCols:int=5;
		//	public var dataStart:int=10;
		protected var dateFormat:String="DD/MM/YYYY";

		final public function loadFile(filePath:String):String
		{
			var sqlFile:File=File.applicationDirectory.resolvePath(filePath);
			if (sqlFile.exists)
			{
				var _sqlConnection:SQLConnection=new SQLConnection();
				var _statement:SQLStatement=new SQLStatement();
				var fileStream:FileStream=new FileStream();
				fileStream.open(sqlFile, FileMode.READ);
				var fileInfo:String=fileStream.readUTFBytes(fileStream.bytesAvailable);
				fileStream.close();
			}
			return fileInfo;
		}

		final protected function loadExpenseDataQIF(fileData:String):Array
		{
			var delimiter:String="\n";
			//select datestamp,0,account,category,abs(amount),exrate,memo,instru from expensetable where category not in (10,11) and type ="withdrawal"
			//union
			//  select datestamp,1,account,category,amount,exrate,memo,instruexpensetable from expensetable where category not in (10,11) and type ="deposit"
			var arrayData:Array=new Array();
			var parts:Array=fileData.split('^');
//			_statement.sqlConnection=_sqlConnection;
			for (var i:int; i < parts.length; i++)
			{
				var dataParts:Array=new Array();
				dataParts=parts[i].toString().split(delimiter);
				try
				{
					var obj:Object=new Object();
					var str:String=new String();
					for (var ij:int=0; ij < dataParts.length; ij++)
					{
						str=dataParts[ij];
						switch (str.charAt(0))
						{
							case "D":
								obj.date=str.substr(1, str.length);
								break;
							case "T":
							{
								if (str.substr(1, 1) == "-")
								{
									obj.type=CommonConstants.WITHDRAWAL;
									obj.amount=str.substr(2, str.length);
								}
								else
								{
									obj.amount=str.substr(1, str.length);
									obj.type=CommonConstants.DEPOSIT;
								}
								break;
							}
							case "N":
								obj.instru=str.substr(1, str.length);
								break;
							case "P":
								obj.memo=str.substr(1, str.length);
								break;
						}
					}
					arrayData.push(obj);
				}
				catch (error:SQLError)
				{
					trace("Record id " + i + " : 	" + error.details);
						//
						//return;
				}
			}
			return arrayData;
		}

		final public function createExpenseObject(date:String, amount:Number, memo:String, txnType:int, account:int, category:int, instr:String):Object
		{
			var obj:Object=new Object();
			obj.account=account;
			obj.category=24;
			for each (var catObj:Object in categoryMatch)
			{
				if (memo.toUpperCase().indexOf(catObj.matchString.toUpperCase()) != -1)
					obj.category=catObj.catid;
			}
			obj.amount=amount;
			obj.payee=0;
			obj.memo=memo;
			obj.type=txnType;
			obj.instru=instr;
			obj.date=DateUtils.getDateFromString(dateFormat, date);
			obj.exRate=1.0;
			return obj;
		}

		final protected function loadExpenseData(fileData:String, delimiter:String, numCols:int):Array
		{
			//select datestamp,0,account,category,abs(amount),exrate,memo,instru from expensetable where category not in (10,11) and type ="withdrawal"
			//union
			//  select datestamp,1,account,category,amount,exrate,memo,instruexpensetable from expensetable where category not in (10,11) and type ="deposit"
			var arrayData:Array=new Array();
			var parts:Array=fileData.split('\n');
//			_statement.sqlConnection=_sqlConnection;
			for (var i:int; i < parts.length; i++)
			{
				var dataParts:Array=new Array();
				dataParts=parts[i].toString().split(delimiter);
				try
				{
					if (dataParts.length >= numCols) //the file can have more columns :)
					{
						var obj:Object=new Object();
						for (var ij:int=0; ij < dataParts.length; ij++)
						{
							obj[ij]=dataParts[ij];
						}
						arrayData.push(obj);
					}
				}
				catch (error:SQLError)
				{
					trace("Record id " + i + " : 	" + error.details);
						//
						//return;
				}
			}
			return arrayData;
		}
	}
}